<template>
	<view class="container">
		<tui-tabs :tabs="tabs" :isFixed="true" :currentTab="currentTab" @change="change" bottom="50%" :bgColor="bgColor"
		 color="#fff" selectedColor="#EB0909" :sliderWidth="146" :sliderHeight="52" sliderBgColor="#fff" :unlined="true"  itemWidth="50%"></tui-tabs>

		<view class="coupon-list">
			<view class="coupon-item" v-for="(item,index) in couponList" :key="index">
				<view class="qrcode-box">
					<view class="qrcode">
						<image src="/static/images/mall/fangwu.jpg" class="tui-slide-image" mode="scaleToFill"></image>
					</view>
				</view>
				<view class="coupon">
					<image src="../../../static/images/index/img_fuwuquan_blue_3x.png" class="coupon-img"></image>
					<view class="circle-left"></view>
					<view class="circle-right"></view>
					<view class="left-tit-box">
						<view class="tit">{{(rooms.filter(itemSon => itemSon.roomId ===item.roomId)[0] || {}).nameHome || ''}}</view>
						<view class="term">类型：{{item.typeCdName}}</view>
						<view class="term">状态：{{item.stateName}}</view>
					</view>
					<view class="right-detail" @tap="spread" :data-index="index">
						<text class="detail-txt">查看详情</text>
						<image :src="'../../../static/images/index/'+(item.spread?'home_icon_choose_3x':'icon_next_3x')+'.png'" :class="[item.spread?'spread':'arrow']"></image>
					</view>
				</view>
				<view class="hidden-box" v-show="item.spread">
					
					<view class="list-item">
						<view class="item-tit">投诉人:</view>
						<view class="item-con">{{item.complaintName}}</view>
					</view>
					<view class="list-item">
						<view class="item-tit">投诉内容:</view>
						<!-- <view class="item-con">{{item.context}}</view> -->
					</view>
					<view class="list-item">
						<view class="item-con">{{item.context}}</view>
					</view>
<!-- 
					<view class="explain">使用说明</view>
					<view class="explain-text" v-for="(items,index) in item.useDescribe" :key="items">{{items}}</view> -->
				</view>

			</view>
            <view class="noData" v-if="!couponList.length">暂无数据</view>
		</view>
		<view class="none" v-show="couponNum==0">暂无可用洗车券~</view>
		<!--加载loadding-->
		<tui-loadmore :visible="loadding" :index="3" type="primary"></tui-loadmore>
		<tui-nomore :visible="!pullUpOn" bgcolor="#fff" text="没有更多评论"></tui-nomore>
		<!--加载loadding-->
		<view class="tui-safearea-bottom"></view>
	</view>
</template>

<script>
	import tuiTabs from "@/components/tui-tabs/tui-tabs"
	const util = require('../../../utils/util.js')
	const qrCode = require('../../../libs/weapp-qrcode.js');
	import tuiLoadmore from "@/components/loadmore/loadmore"
	import tuiNomore from "@/components/nomore/nomore"
	export default {
		components: {
			tuiTabs,
			tuiLoadmore,
			tuiNomore
		},
		data() {
			return {
				currentTab: 0,
				bgColor: "linear-gradient(90deg, rgb(255, 118, 38), rgb(252, 30, 82))",
				tabs: [{
					name: "正在处理"
				}, {
					name: "处理完成"
				}],
				rooms:[],
				show: false,
				couponNum: 4,
				waitRepair:[], // 0
				doingRepair:[], // 1
				repaired:[], // 2
				loadding: false,
				pullUpOn: true,
				mappingList:['waitRepair','doingRepair','repaired'],
				couponList:[],
				page:1,
				// couponList: [{
				// 	name: "购物券",
				// 	code: "xyz0900100200",
				// 	invalidTime: "2019-07-01",
				// 	// #ifdef APP-PLUS || MP
				// 	spread: true,
				// 	// #endif
				// 	// #ifdef H5
				// 	spread: false,
				// 	// #endif
				// 	sendTime: "2019-06-01",
				// 	suitStore: "全部",
				// 	useDescribe: ["1、可在任何适用商家内消费", "2、解释权归Thor所有"]
				// }, {
				// 	name: "打车券",
				// 	code: "xyz0900100300",
				// 	invalidTime: "2019-07-01",
				// 	spread: false,
				// 	sendTime: "2019-06-01",
				// 	suitStore: "滴滴打车",
				// 	useDescribe: ["1、可在任何适用商家内消费", "2、解释权归Thor所有"]
				// }, {
				// 	name: "餐饮券",
				// 	code: "xyz0900100400",
				// 	invalidTime: "2019-07-01",
				// 	spread: false,
				// 	sendTime: "2019-06-01",
				// 	suitStore: "麦当劳",
				// 	useDescribe: ["1、可在任何适用商家内消费", "2、解释权归Thor所有"]
				// }, {
				// 	name: "服务券",
				// 	code: "xyz0900100500",
				// 	invalidTime: "2019-07-01",
				// 	spread: false,
				// 	sendTime: "2019-06-01",
				// 	suitStore: "如家按摩店",
				// 	useDescribe: ["1、可在任何适用商家内消费", "2、解释权归Thor所有"]
				// }],
				qrcode_w: uni.upx2px(240)
			}
		},
		computed:{
		   	
		},
		onLoad: function(options) {
			uni.getSystemInfo({ // 判断系统
				success: (res) => {
					if (res.system.indexOf('iOS') > -1 || res.platform.indexOf('ios') > -1) {
						this.show = true
					}
				}
			});
			let that = this;
			    this.java110Context.getRooms().then(function (res) {
			      let _owner = res.data.owner;
			
			      let _rooms = res.data.rooms;
			      if(_rooms == null || _rooms == undefined || _rooms.length == 0){
			        return ;
			      }
			
			      let _roomId = '';
			      let roomCloums = [];
			      _rooms.forEach(function(_item){
					 _item.nameHome = (_item.floorNum + "号楼" + _item.unitNum + "单元" + _item.roomNum + "室");
			        _roomId += (_item.roomId+',');
					roomCloums.push(_item)
			      });
			      
			      if(_roomId != ''){
			        _roomId = _roomId.substring(0,_roomId.length-1);
			      }
			
			      that.setData({
					rooms:roomCloums,
			        communityId: _owner.communityId,
			        ownerId: _owner.memberId,
			        roomId: _roomId
			      });
			      that._loadRepair(that.currentTab);
			    });
			    
			//const W = uni.getSystemInfoSync().windowWidth;
			//const qrcode_w = uni.upx2px(260);
			//this.qrcode_w = qrcode_w;
			// #ifdef APP-PLUS || MP
			this.couponQrCode(this.couponList[0].code, "couponQrcode0")
			// #endif
		},
		// 页面上拉触底事件的处理函数
		onReachBottom: function() {
			if (!this.pullUpOn) return;
			this.loadding = true
			const keyList = ['waitRepair','doingRepair']
			let listKey = keyList[this.currentTab] || 'repaired';
			if (this.page*10 > this.couponList.length) {
				this.loadding = false;
				this.pullUpOn = false
			} else {
				this.page = this.page + 1;
				this.getList();
			}
		},
		methods: {
			setData(obj){
				Object.keys(obj).forEach(key => {
					this[key] = obj[key]
				})
			},
			_loadRepair(_active){
				let that = this;
				
				    let _state = '';
				    if (_active == 0){
				      _state = '10001';
				    }else if(_active == 1){
				      _state = '10002';
				    }else{
				      _state = '1200';
				    }
				   
				    let _paramIn = {
				      "communityId": that.communityId,
				      "ownerId": that.ownerId,
				      "roomId":that.roomId,
				      "state":_state,
				      "page":this.page,
				      "row":10
				    };
					uni.showLoading({
						mask: true,
						title: '请稍候...'
					})
				    this.java110Context.request({
				      url: this.java110Constant.url.listComplaints,
				      header: this.java110Context.getHeaders(),
				      method: "GET",
				      data: _paramIn,
				      success: function (res) {
						uni.hideLoading();
				        if (res.statusCode == 200) {
				          let _ownerRepairs = res.data.complaints;
				          if (_active == 0){
				            that.setData({
				              waitRepair: _ownerRepairs
				            });
				          } else if (_active == 1){
				            that.setData({
				              doingRepair: _ownerRepairs
				            });
				          }else{
				            that.setData({
				              repaired: _ownerRepairs
				            });
				          }
						  let couponList = that[that.mappingList[that.currentTab]];
						  that.couponList = couponList.map(item => {
							  item.spread = false
							  return item
						  })
				          return;
				        }
				
				        uni.showToast({
				          title: "查询报修单失败",
				          icon: 'none',
				          duration: 2000
				        });
				      },
				      fail: function (e) {
						uni.hideLoading();
				        uni.showToast({
				          title: "服务器异常了",
				          icon: 'none',
				          duration: 2000
				        })
				      }
				    })
			},
			change(e) {
				this.currentTab = e.index
				this.page = 1;
				const keyList = ['waitRepair','doingRepair']
				let listKey = keyList[this.currentTab] || 'repaired';
				this.couponList = [];
				this.loadding = false;
				this.pullUpOn = true;
				this._loadRepair(this.currentTab);
			},
			spread: function(e) {
				let index = Number(e.currentTarget.dataset.index)
				let couponList = this.couponList
				// debugger
				// if (!couponList[index].spread) {
				// 	setTimeout(()=>{
				// 		this.couponQrCode(couponList[index].code, "couponQrcode" + index)
				// 	},50)
				// }
				couponList[index].spread = !couponList[index].spread;
				this.couponList = [...couponList]
			},
			// 二维码生成工具
			couponQrCode(text, canvasId) {
				new qrCode(canvasId, {
					text: text,
					width: this.qrcode_w,
					height: this.qrcode_w,
					colorDark: "#333333",
					colorLight: "#FFFFFF",
					correctLevel: qrCode.CorrectLevel.H
				});
				if (canvasId == "couponQrcode0") {
					setTimeout(() => {
						if (!this.show) {
							this.show = true
						}
					}, 0)
				}
			}
		}
	}
</script>

<style>
	page {
		background: #fff;
	}

	.container {
		padding-bottom: env(safe-area-inset-bottom);
	}

	.header {
		width: 100%;
		padding: 0 30upx;
		box-sizing: border-box;
		font-size: 24upx;
		color: #999;
		height: 54upx;
		line-height: 54upx;
		background: #fff8d5;
		display: flex;
		align-items: center;
		position: fixed;
		top: 0;
		/* #ifdef H5 */
		top: 44px;
		/* #endif */
		z-index: 999999
	}


	.coupon-num {
		color: #5677fc;
	}

	.coupon-list {
		width: 100%;
		padding: 104upx 50upx;
		box-sizing: border-box;
	}

	.coupon-item {
		margin-top: 20upx;
		width: 100%;
		box-shadow: 0 0 12upx 0 #eaeef1;
		border-radius: 2upx;

	}

	.coupon {
		height: 160upx;
		position: relative;
		display: flex;
		align-items: center;
		/* background: red */
	}

	.coupon-img {
		width: 100%;
		height: 160upx;
		position: absolute;
		left: 0;
		top: 0;
		z-index: 0;
		background: #fff
	}

	.circle-left {
		position: absolute;
		left: -28upx;
		top: 64upx;
		height: 36upx;
		width: 36upx;
		border-radius: 18upx;
		background: #fff
	}

	.circle-right {
		position: absolute;
		top: 64upx;
		right: -25upx;
		height: 36upx;
		width: 36upx;
		border-radius: 18upx;
		background: #fff
	}

	.left-tit-box {
		width: 414upx;
		padding-left: 26upx;
		box-sizing: border-box;
		z-index: 10
	}

	.tit {
		width: 290upx;
		font-size: 30upx;
		color: #333;
		line-height: 42upx;
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-line-clamp: 2;
		-webkit-box-orient: vertical;
	}

	.term {
		width: 228upx;
		font-size: 24upx;
		color: #999;
		line-height: 24upx;
		padding-top: 7upx;
		white-space: nowrap;
	}

	.right-detail {
		width: 236upx;
		text-align: center;
		height: 160upx;
		display: flex;
		align-items: center;
		justify-content: center;
		z-index: 10
	}

	.detail-txt {
		font-size: 24upx;
		color: #fff;
		padding-right: 12upx;
	}

	.arrow {
		width: 16upx;
		height: 24upx;
	}

	.spread {
		width: 24upx;
		height: 16upx;
	}

	
	.tui-slide-image {
		width: 100%;
		height: 240rpx;
		display: block;
		border-radius: 12rpx 12rpx 0 0;
		/* transition: all 0.1s linear; */
	}

	.hidden-box {
		padding: 35upx 40upx 27upx 40upx;
		box-sizing: border-box;
		color: #333;
		border-top: 1upx solid #EAEEF1;
	}

	.code-tit {
		font-size: 28upx;
		line-height: 28upx;
	}

	.code-num {
		font-size: 30upx;
		line-height: 30upx;
		padding-top: 23upx;
		font-weight: bold;
	}

	.qrcode-box {
		width: 100%;
		border-bottom: 1upx dashed #eaeef1;
		margin-bottom: 6upx;
	}

	.list-item {
		height: 90upx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		font-size: 28upx;
	}

	.list-item::after {
		left: 0;
	}

	.item-tit {
		color: #666;
	}

	.item-con {
		color: #333;
	}

	.explain {
		font-size: 28upx;
		line-height: 28upx;
		color: #666;
		padding: 33upx 0 25upx 0
	}

	.explain-text {
		font-size: 28upx;
		line-height: 41upx;
		color: #333;
	}

	.none {
		width: 100%;
		position: fixed;
		text-align: center;
		font-size: 30upx;
		color: #ccc;
		top: 49%;
	}
</style>
